Lines 0--6 are the import statements necessary to use the Java R-GMA
classes.

Lines 10--15 create a consumer with a continuous query
of \texttt{SELECT userId, aString, aReal, anInt, RgmaTimestamp FROM
default.userTable}. In this case we have a continuous query which will
start with available published tuples from the last 10 minutes and
receive any new tuples published. It is possible to leave out the
\texttt{historyPeriod} and use \texttt{QueryType}
instead of \texttt{QueryTypeWithInterval}, in which case only newly
published tuples will be received. The query has a timeout of 5
minutes. With a latest or history query we can expect the query to
complete within the timeout. However the continuous query can only be
terminated by an explicit abort call or by the timeout being
exceeded. So we expect this query to take exactly 5 minutes.

Lines 16--32 retrieve all of the results for the query. While there is
data available it will loop without delay. If there is no data
currently available it will sleep for 2 seconds before trying
\texttt{pop} again. The 2000 is the maximum number of tuples to be
returned at once.

Lines 25--30 extract individual fields from the tuple and print them.

Line 33 closes the consumer.

Lines 34--37 report any R-GMA exceptions that may occur.

\subsubsection {One-off Queries}
For one-off queries, either history or latest, it is preferable to
check to see if the query aborted. This can be achieved by looking to
see if \texttt{consumer.hasAborted()} is true after either kind of
consumer loop. This can be the result of hitting the timeout (5
minutes in the above case) or making an
explicit \texttt{consumer.abort()} call. Note that continuous
queries \emph{only} stop by one of these means so there is no point in
checking in that case.

\subsubsection{Resilient Consumer Example}
This example illustrates how to write a resilient consumer according
to the recommendations in section \ref{sec:advice}.  The consumer
retrieves information periodically. If there is a temporary problem it
retries after one minute.

\input{ResilientConsumer-java}

Lines 0--7 are the import statements necessary to use the Java R-GMA
classes.

Lines 10--11 set up the consumers select statement.

Line 13 set up the history period for the query.

Line 14 set up initial state for the consumer.

Lines 15--24 create a new consumer. This uses a continuous information
query starting from 30 seconds in the past so that if the consumer is
restarted data should not be lost. N.B. It is possible that this may
cause some tuples to be retrieved a second time if the system has to
restart the consumer. If there is an RGMATemporaryException then sleep
for 60 seconds before retrying to recreate the consumer.

Lines 25--46 loops forever - each iteration will retrieve data and
check for warnings. While there is data available it will loop without
delay. If there is no data currently available it will sleep for 2
seconds before trying again. See Section \ref{sec:adviceConsumers} for
further explanation about the pop and sleep. If there is an
RGMATemporaryException then sleep for 60 seconds before trying to pop
again.

Lines 31--36 extract individual fields from the tuple and print them.

Lines 47--50 report an RGMAPermanentException. As this is considered
to be a permanent problem there is no point retrying the operation.
